package entity

import (
	"encoding/json"
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
)

const (
	TbNotice db.TbName = "notices"
)

const (
	NoticeEndTime db.TbCol = "end_time"
)

type Notice struct {
	migrate.OneModel
	TypeId    uint8     `gorm:"comment:类型" json:"typeId"`
	StartTime time.Time `gorm:"comment:开始时间" json:"startTime"`
	EndTime   time.Time `gorm:"comment:结束时间" json:"endTime"`
	LoopTime  int       `gorm:"comment:循环时间" json:"loopTime"`
	Param     string    `gorm:"comment:参数" json:"param"`
}

func NewNotice() *Notice {
	ret := &Notice{}
	ret.UpdatedAt = time.Now()
	ret.CreatedAt = time.Now()
	return ret
}

func (receiver *Notice) GetParam() []any {
	if receiver.Param != "" {
		var arrRet []any
		json.Unmarshal([]byte(receiver.Param), &arrRet)
		return arrRet
	}
	return []any{}
}

func (receiver *Notice) CanSend() bool {
	now := time.Now()
	return now.After(receiver.StartTime) && now.Before(receiver.EndTime)
}

func initNotice() {
	migrate.AutoMigrate(&Notice{})
}
